package com.finance.report.controller;

import com.finance.base.constant.BaseConstants;
import com.finance.framework.controller.BaseController;
import com.finance.framework.controller.Result;
import com.finance.framework.file.FileUtils;
import com.finance.framework.lang.Page;
import com.finance.framework.lang.PageResultBO;
import com.finance.report.bean.export.ManSalaryParse;
import com.finance.report.bean.export.ManSalaryParseBean;
import com.finance.report.bean.pojo.*;
import com.finance.report.request.LogisticsViewReq;
import com.finance.report.request.ManSalaryReq;
import com.finance.report.service.IManSalaryService;
import com.finance.util.DateUtil;
import com.finance.util.Excel.ExcelUtil;
import com.finance.util.export.ExportBuilder;
import com.finance.util.export.ExportFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Author: gly
 * @Date: Created in 14:54 2017/4/20
 * 员工月结报表
 */
@Controller
@RequestMapping("/report/mansalary")
public class ManSalaryController extends BaseController {

    @Resource
    IManSalaryService manSalaryService;


    @RequestMapping("/index.htm")
    public String index(HttpServletRequest request) {
        String fileUrl = BaseConstants.getOssProperties(BaseConstants.FILE_URL);
        this.logger.info("fileUrl:{}",fileUrl);
        request.setAttribute("fileUrl",fileUrl);
        return "report/mansalary/index";
    }

    @RequestMapping("/list")
    public Result list(ManSalaryReq form,HttpServletRequest request) {
        Page page = Page.getInstance(form);
        ManSalaryList manSalaryList = new ManSalaryList();
        manSalaryList.setUserName(form.getUserName());
        manSalaryList.setOrgName(form.getOrgName());
        page.setParams(manSalaryList);
        PageResultBO<ManSalary> resultBO = manSalaryService.listByPage(page);
        return getSuccessResult(resultBO);
    }

    @RequestMapping("/lists")
    public Result mansalaryList(ManSalaryReq form) {
        Page page = Page.getInstance(form);
        ManSalaryList manSalaryList = new ManSalaryList();
        manSalaryList.setWorkNo(form.getWorkNo());
        manSalaryList.setUserName(form.getUserName());
        manSalaryList.setOrgName(form.getOrgName());
        manSalaryList.setSupplierName(form.getSupplierName());
        manSalaryList.setCarrierName(form.getCarrierName());
        manSalaryList.setToProvinceName(form.getToProvinceName());
        manSalaryList.setSuffix(form.getSuffix());
        page.setParams(manSalaryList);
        PageResultBO<Map<String,Object>> resultBO = manSalaryService.listSalaryByPage(page);
        return getSuccessResult(resultBO);
    }

    /**
     * 查询出配送员运单信息
     *
     * @return
     */
    @RequestMapping(value = "/showorders", method = RequestMethod.GET)
    public String update(String userCode, String month, HttpServletRequest request) {
        LogisticsViewReq logisticsViewReq=new LogisticsViewReq();
        logisticsViewReq.setUserCode(userCode);
        logisticsViewReq.setSalaryMonth(month);
        request.setAttribute("logisticsViewReq",logisticsViewReq);
        return "/report/mansalary/showorders";
    }

    @RequestMapping(value = "/showorders", method = RequestMethod.POST)
    public Result ordersInfos(LogisticsViewReq form) {
        Page page = Page.getInstance(form);
        LogisticsViewList logisticsViewList = new LogisticsViewList();
        logisticsViewList.setUserCode(form.getUserCode());
        String suffix = form.getSalaryMonth().replace("-", "").trim().substring(2, 6);
        String tablename = BaseConstants.SYSCONFIG_KEY_LOGISTICDATA_TABLE + suffix;
        logisticsViewList.setSalaryMonth(form.getSalaryMonth());
        logisticsViewList.setTableName(tablename);
        page.setParams(logisticsViewList);
        PageResultBO<LogisticsDataView> resultBO = manSalaryService.getOrdersDetails(page);
        return getSuccessResult(resultBO);
    }

    /**
     * 查询出配费详细信息
     *
     * @return
     */
    @RequestMapping(value = "/showdetails")
    public String salaryInfos(String userCode, String salaryMonth, HttpServletRequest request) {
        List<ManSalaryDetail> salaryDetails = manSalaryService.getSalaryDetails(userCode, salaryMonth);
        request.setAttribute("salaryDetails", salaryDetails);
        request.setAttribute("userCode", userCode);
        request.setAttribute("salaryMonth", salaryMonth);
        return "report/mansalary/showdetails";
    }

    @RequestMapping("/export")
    public void export(ManSalaryReq form, HttpServletResponse response) {
        Page page = Page.getInstance(form);
        ManSalaryList manSalaryList = new ManSalaryList();
        manSalaryList.setOrgName(form.getOrgName());
        manSalaryList.setOrgName(form.getOrgName());
        page.setParams(manSalaryList);
        page.setLoadAll(true);
        PageResultBO<ManSalary> resultBO = manSalaryService.listByPage(page);
        try {
            ExportBuilder builder = new ExportBuilder(ManSalaryParseBean.class, new ManSalaryParse());
            ExportFactory factory = ExportFactory.getExportFactory(builder);
            factory.setDataList(resultBO.getDataList()).outPut(response);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
    }

    @RequestMapping(value = "/down", method = RequestMethod.GET)
    public String down(String userCode, String salaryMonth, HttpServletRequest request) {
        Map<String,String> downParams = new HashMap<String, String>();
        downParams.put("userCode",userCode);
        downParams.put("salaryMonth",salaryMonth);
        request.setAttribute("downParams",downParams);
        return "/report/mansalary/down";
    }

    @RequestMapping(value = "/downData",method = RequestMethod.GET)
    public void down(String userCode, String salaryMonth,HttpServletResponse response){
        String filekey = BaseConstants.FINANCE_REPORT_FILE_PATH+BaseConstants.FINANCE_FILE_SEPORATOR+salaryMonth+"-"+userCode+BaseConstants.DEFAULT_EXPORT_FILE_SUFFIX;
        FileUtils.downFile(filekey,response);
    }

    /**
     * 判断文件是否存在
     */
    @RequestMapping(value ="/exist",method = RequestMethod.POST)
    public Result exist(String userCode,String salaryMonth){
        String buckName = BaseConstants.getOssProperties(BaseConstants.OSS_BUCKET_NAME);
        String fileKey = BaseConstants.FINANCE_REPORT_FILE_PATH + "/" + salaryMonth + "-" + userCode + BaseConstants.DEFAULT_EXPORT_FILE_SUFFIX;
        return getSuccessResult(FileUtils.exist(buckName,fileKey));
    }

    /**
     * 导出配送员月配费详情报表
     * @param userCode
     * @param salaryMonth
     * @param response
     */
    @RequestMapping(value = "/exportDetail", method = RequestMethod.POST)
    public void export(String userCode ,String salaryMonth,HttpServletResponse response){
        response.setHeader("content-disposition", "attachment;filename=" +"ManSalaryDetail—"+ DateUtil.dateFormat(new Date(),"yyyy-MM-dd").toString()+".xlsx");
        try {
            List<ManSalaryDetail> manSalaryDetailList = manSalaryService.getSalaryDetails(userCode, salaryMonth);
            OutputStream out = response.getOutputStream();
            ExcelUtil<ManSalaryDetail> excelUtil = new ExcelUtil<ManSalaryDetail>(ManSalaryDetail.class);
            excelUtil.exportExcel(manSalaryDetailList,"配送员配送运单详情",out);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
